/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_cyclonedds/listener_impl/DataWriterListenerImpl.hpp"
#include "dal_cyclonedds/entity_impl/DalDataWriterImpl.hpp"
#include "dal_cyclonedds/utils/log.hpp"

static void on_liveliness_lost_writer(dds_entity_t writer, 
    const dds_liveliness_lost_status_t status, void* arg)
{
    dal_datawriter_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_datawriter_listener_callbacks_t*>(arg);
    if (callbacks_->on_liveliness_lost == nullptr)
        return;

    dal_datawriter_t dal_writer;
    dal_liveliness_lost_status_t dal_status;
    DalDataWriterImpl* writer_impl = new DalDataWriterImpl(writer, nullptr);
    if (!writer_impl) {
        LOG_ERROR() << "DalDataWriterImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_writer.handle_ = writer_impl;
    StatusConverter::GetInstance()->ConvertLivelinessLostStatusFromCycloneddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_liveliness_lost)(&dal_writer, &dal_status, data);
    delete writer_impl;
}

static void on_offered_deadline_missed_writer(dds_entity_t writer, 
    const dds_offered_deadline_missed_status_t status, void* arg)
{
    dal_datawriter_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_datawriter_listener_callbacks_t*>(arg);
    if (callbacks_->on_offered_deadline_missed == nullptr)
        return;

    dal_datawriter_t dal_writer;
    dal_offered_deadline_missed_status_t dal_status;
    DalDataWriterImpl* writer_impl = new DalDataWriterImpl(writer, nullptr);
    if (!writer_impl) {
        LOG_ERROR() << "DalDataWriterImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_writer.handle_ = writer_impl;
    StatusConverter::GetInstance()->ConvertOfferedDeadlineMissedStatusFromCycloneddsToDal(&status,
                                                                                       &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_offered_deadline_missed)(&dal_writer, &dal_status, data);
    delete writer_impl;
}

static void on_offered_incompatible_qos_writer(dds_entity_t writer, 
    const dds_offered_incompatible_qos_status_t status, void* arg)
{
    dal_datawriter_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_datawriter_listener_callbacks_t*>(arg);
    if (callbacks_->on_offered_incompatible_qos == nullptr)
        return;

    dal_datawriter_t dal_writer;
    dal_offered_incompatible_qos_status_t dal_status;
    DalDataWriterImpl* writer_impl = new DalDataWriterImpl(writer, nullptr);
    if (!writer_impl) {
        LOG_ERROR() << "DalDataWriterImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_writer.handle_ = writer_impl;
    StatusConverter::GetInstance()->ConvertOfferedIncompatibleQosStatusFromCycloneddsToDal(
        &status, &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_offered_incompatible_qos)(&dal_writer, &dal_status, data);
    delete writer_impl;
}

static void on_publication_matched_writer(dds_entity_t writer, 
    const dds_publication_matched_status_t  status, void* arg)
{
    dal_datawriter_listener_callbacks_t *callbacks_ 
        = reinterpret_cast<dal_datawriter_listener_callbacks_t*>(arg);
    if (callbacks_->on_publication_matched == nullptr)
        return;

    dal_datawriter_t dal_writer;
    dal_publication_matched_status_t dal_status;
    DalDataWriterImpl* writer_impl = new DalDataWriterImpl(writer, nullptr);
    if (!writer_impl) {
        LOG_ERROR() << "DalDataWriterImpl memory allocation failed." << LOG_ENDL;
        return;
    }
    dal_writer.handle_ = writer_impl;
    StatusConverter::GetInstance()->ConvertPublicationMatchedStatusFromCycloneddsToDal(&status,
                                                                                    &dal_status);

    void* data = callbacks_->data_;
    (callbacks_->on_publication_matched)(&dal_writer, &dal_status, data);
    delete writer_impl;
}


DataWriterListenerImpl::DataWriterListenerImpl(dal_datawriter_listener_callbacks_t callbacks)
    : callbacks_(callbacks) {}

DataWriterListenerImpl::~DataWriterListenerImpl()
{
    // c_listener release while participant is releasing.
}

dds_listener_t* DataWriterListenerImpl::CycloneDDSListener()
{
    if (c_listener_ == nullptr) {
        c_listener_ = dds_create_listener(NULL);
    }
    dds_lset_liveliness_lost_arg(c_listener_, on_liveliness_lost_writer, &callbacks_, true);
    dds_lset_offered_deadline_missed_arg(c_listener_, on_offered_deadline_missed_writer, &callbacks_, true);
    dds_lset_offered_incompatible_qos_arg(c_listener_, on_offered_incompatible_qos_writer, &callbacks_, true);
    dds_lset_publication_matched_arg(c_listener_,  on_publication_matched_writer, &callbacks_, true);
    return c_listener_;
}